//
//  HOMNSDictionarySelectWhereTests.m
//  HigherOrderMessaging
//
//  Created by Ofri Wolfus on 12/10/06.
//  Copyright 2006 Ofri Wolfus. All rights reserved.
//

#import "HOMNSDictionarySelectWhereTests.h"


@implementation HOMNSDictionarySelectWhereTests

- (id)init {
	if ((self = [super init])) {
		int i;
		
		msg1 = [MSG(hasPrefix:@"a") retain];
		msg2 = [MSG(hasPrefix:@"1") retain];
		
		for (i = 1; i < 10000; i++) {
			NSString *str = [NSString stringWithFormat:@"%d", i];
			
			if ([str hasPrefix:@"1"])
				[largeDictResult setObject:str forKey:[NSNumber numberWithUnsignedInt:i]];
		}
	}
	
	return self;
}

- (void)dealloc {
	[msg1 release]; msg1 = nil;
	[msg2 release]; msg2 = nil;
	[super dealloc];
}

#pragma mark -
#pragma mark Small Dictionary Tests

- (id)testSmallDictionary {
	return [smallDict selectWhere:MSG(hasPrefix:@"a"), nil];
}

- (id)testSmallDictionary_CachedMessage {
	return [smallDict selectWhere:msg1, nil];
}

- (id)testSmallDictionary_objectAtIndex {
	int i;
	NSMutableDictionary *r = [NSMutableDictionary dictionary];
	NSArray	*keys = [smallDict allKeys];
	unsigned c = [smallDict count];
	NSString *str;
	
	for (i = 0; i < c; i++) {
		id key = [keys objectAtIndex:i];
		str = [smallDict objectForKey:key];
		
		if ([str hasPrefix:@"a"])
			[r setObject:str forKey:key];
	}
	
	return r;
}

- (id)testSmallDictionary_NSEnumerator {
	NSMutableDictionary *r = [NSMutableDictionary dictionary];
	NSEnumerator *e = [smallDict keyEnumerator];
	id key;
	NSString *str;
	
	while ((key = [e nextObject])) {
		str = [smallDict objectForKey:key];
		
		if ([str hasPrefix:@"a"])
			[r setObject:str forKey:key];
	}
	
	return r;
}

#pragma mark -
#pragma mark Large Dictionary Tests

- (id)testLargeDictionary {
	return [largeDict selectWhere:MSG(hasPrefix:@"1"), nil];
}

- (id)testLargeDictionary_CachedMessage {
	return [largeDict selectWhere:msg2, nil];
}

- (id)testLargeDictionary_objectAtIndex {
	int i;
	NSMutableDictionary *r = [NSMutableDictionary dictionary];
	NSArray	*keys = [largeDict allKeys];
	unsigned c = [largeDict count];
	NSString *str;
	
	for (i = 0; i < c; i++) {
		id key = [keys objectAtIndex:i];
		str = [largeDict objectForKey:key];
		
		if ([str hasPrefix:@"1"])
			[r setObject:str forKey:key];
	}
	
	return r;
}

- (id)testLargeDictionary_NSEnumerator {
	NSMutableDictionary *r = [NSMutableDictionary dictionary];
	NSEnumerator *e = [largeDict keyEnumerator];
	id key;
	NSString *str;
	
	while ((key = [e nextObject])) {
		str = [largeDict objectForKey:key];
		
		if ([str hasPrefix:@"1"])
			[r setObject:str forKey:key];
	}
	
	return r;
}

#pragma mark -
#pragma mark Tests Verification

- (BOOL)verifyResult:(NSDictionary *)dict ofTest:(SEL)sel {
	NSString *name = NSStringFromSelector(sel);
	
	if ([name hasPrefix:@"testSmallDictionary"]) {
		DPTestAssert([dict count] == 2U, @"The result doesn't contain 2 objects. Result = %@", dict);
		DPTestAssert([[dict objectForKey:[NSNumber numberWithUnsignedInt:1]] isEqualToString:@"a"], @"The result doesn't contain 'a'. Result = %@", dict);
		DPTestAssert([[dict objectForKey:[NSNumber numberWithUnsignedInt:3]] isEqualToString:@"abc"], @"The result doesn't contain 'abc'. Result = %@", dict);
	}
	
	return [super verifyResult:dict ofTest:sel];
}

@end
